from django.shortcuts import render

# Create your views here.
from django.shortcuts import render
from django.shortcuts import HttpResponse

from login import models  # 导入models文件


def index(request):  # 第一个参数必须是request，建议不更改名字

    # return HttpResponse('This is my first page')  #返回的字符串用这个类封装起来才能被http协议识别
    return render(request, 'index.html')  # 返回的字符串用这个类封装起来才能被http协议识别


'''
# 基于传值示例
def input(request):
    if request.method == 'POST':
        print('这是post提交页面')
        username = request.POST.get('username')
        password = request.POST.get('password')
        print(username, password)
    elif request.method == 'GET':
        print('这是一个get传值')
    return render(request, 'input.html')
'''
user_list = []  # 创建一个空列表，用来存放数据库中取出来的数据


def input(request):
    if request.method == 'POST':
        print('这是一个post提交')
        username = request.POST.get('username')
        password = request.POST.get('password')
        print(username, password)
        # 将用户输入数据存到数据库中
        models.UserInfo.objects.create(user=username, pwd=password)

    # 从数据库读取数据
    # user_list = models.UserInfo.objects.all()
    user_list = models.UserInfo.objects.all()

    # return render(request, 'input.html', {'data', user_list})
    # return render(request, 'input.html', {'data': user_list})
    return render(request, 'input.html', {'data': user_list})



